package com.ffapp.rainmonitorcustom.bean;

import android.content.Context;
import android.graphics.Color;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;

import com.ffapp.rainmonitorcustom.R;
import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.utils.ColorTemplate;
import com.github.mikephil.charting.utils.MPPointF;

import java.util.ArrayList;

import my.StringUtil;

public class MonitorDeviceNumBean extends BasisBean {

	/**
	 *
	 */
	private static final long serialVersionUID = 1L;

    /**
     * allnum : 6
     * onLine : 4
     */

    private int allnum;
    private int onLine;

    public int getAllnum() {
        return allnum;
    }

    public void setAllnum(int allnum) {
        this.allnum = allnum;
    }

    public int getOnLine() {
        return onLine;
    }

    public String getPercentStr(){
        if(allnum == 0)return "100%";
        return StringUtil.getPercent(onLine,allnum);
    }

    public void setOnLine(int onLine) {
        this.onLine = onLine;
    }

    public int getOfflineNum(){
        return  allnum-onLine;
    }

    public void setCharPieStatistics(Context mContext, PieChart chartStatistics){
        chartStatistics.setUsePercentValues(false);
        chartStatistics.getDescription().setEnabled(false);
        chartStatistics.setExtraOffsets(5, 3, 5, 5);

//        chartStatistics.setDragDecelerationFrictionCoef(0.95f);
//        chartStatistics.setCenterTextTypeface(mTfLight);
//        chartStatistics.setCenterText(generateCenterSpannableText());

//        chartStatistics.setDrawHoleEnabled(true);
//        chartStatistics.setHoleColor(Color.WHITE);

//        chartStatistics.setTransparentCircleColor(Color.WHITE);
//        chartStatistics.setTransparentCircleAlpha(110);
        chartStatistics.setHoleRadius(80f);
        chartStatistics.setTransparentCircleRadius(20f);

        chartStatistics.setDrawCenterText(true);
        chartStatistics.setRotationAngle(270);
        // enable rotation of the chart by touch
//        chartStatistics.setRotationEnabled(true);
//        chartStatistics.setHighlightPerTapEnabled(true);
        // chartStatistics.setUnit(" €");
        // chartStatistics.setDrawUnitsInChart(true);
        // add a selection listener
//        chartStatistics.setOnChartValueSelectedListener(this);

        setCharPieStatisticsData(mContext,chartStatistics);

        chartStatistics.animateY(1400, Easing.getEasingFunctionFromOption(Easing.EasingOption.EaseInOutElastic));
        // chartStatistics.spin(2000, 0, 360);

        Legend l = chartStatistics.getLegend();
        l.setEnabled(false);
//        l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
//        l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
//        l.setOrientation(Legend.LegendOrientation.VERTICAL);
//        l.setDrawInside(false);
//        l.setXEntrySpace(7f);
//        l.setYEntrySpace(0f);
//        l.setYOffset(0f);
//
//        // entry label styling
//        chartStatistics.setEntryLabelColor(Color.WHITE);
//        chartStatistics.setEntryLabelTextSize(12f);
//

    }

    void setCharPieStatisticsData(Context mContext,PieChart chartStatistics){

        SpannableString s = new SpannableString("在线率\n"+getPercentStr());
        s.setSpan(new ForegroundColorSpan(mContext.getResources().getColor(R.color.base_text_grey)), 0, 3, 0);
        s.setSpan(new RelativeSizeSpan(0.8f), 0, 3, 0);
//        s.setSpan(new StyleSpan(Typeface.NORMAL), 14, s.length() - 15, 0);
        s.setSpan(new ForegroundColorSpan(Color.BLACK), 3, s.length(), 0);
        s.setSpan(new RelativeSizeSpan(1.3f), 3, s.length(), 0);
//        s.setSpan(new StyleSpan(Typeface.ITALIC), s.length() - 14, s.length(), 0);
//        s.setSpan(new ForegroundColorSpan(ColorTemplate.getHoloBlue()), s.length() - 14, s.length(), 0);
        chartStatistics.setCenterText(s);


        ArrayList<PieEntry> entries = new ArrayList<PieEntry>();
        // NOTE: The order of the entries when being added to the entries array determines their position around the center of
        // the chart.
//        entries.add(new PieEntry(25,""));
//        entries.add(new PieEntry(75,""));
        entries.add(new PieEntry(getOnLine(),""));
        entries.add(new PieEntry(getOfflineNum(),""));

        PieDataSet dataSet = new PieDataSet(entries, "statistics");
        dataSet.setDrawIcons(false);
//        dataSet.setSliceSpace(3f);
        dataSet.setIconsOffset(new MPPointF(0, 40));
        dataSet.setSelectionShift(5f);
        ArrayList<Integer> colors = new ArrayList<Integer>();
        colors.add(mContext.getResources().getColor(R.color.char_pie_color_green));
        colors.add(mContext.getResources().getColor(R.color.char_pie_color_grey));
        colors.add(ColorTemplate.getHoloBlue());

        dataSet.setColors(colors);
        //dataSet.setSelectionShift(0f);

        PieData data = new PieData(dataSet);
        data.setDrawValues(false);
//        data.setValueFormatter(new PercentFormatter());
//        data.setValueTextSize(11f);
//        data.setValueTextColor(Color.WHITE);
        chartStatistics.setData(data);

        // undo all highlights
        chartStatistics.highlightValues(null);

        chartStatistics.invalidate();
    }
}
